const app = getApp()
const util = require("../../../util/util.js")

Page({

  data: {
    loading: 1,
    regularSuperviseItemList: [],
    isSupervising: false,
    superviseSchoolName: "",
    superviseChecked: []
  },
  onLoad: function (options) {
    let departmentId = app.globalData.departmentId;
    let userId = app.globalData.userId;
    this.setData({
      loading: 0,
      isSupervising: app.globalData.isSupervising,
      superviseSchoolName: app.globalData.superviseSchoolName
    })
    this.getSuperviseItemList(departmentId, userId);

  },



  getSuperviseItemList: function (departmentId, userId) {
    dd.httpRequest({
      url: app.baseUrl + '/DingDing/Supervise/getEudSuperviseItemList',
      method: "GET",
      data: {
        departmentId: departmentId,
        userId: userId
      },
      dataType: 'json',
      success: res => {
        this.setData({
          regularSuperviseItemList: res.data
        })
      }
    })
  },


  checkSupervise: function (e) {
    this.setData({
      superviseChecked: e.detail.value
    })
  },


  toSchoolSelection: function (e) {
    console.log("e", e)
    let status = e.currentTarget.dataset.status
    let superviseId = e.currentTarget.dataset.superviseid
    let superviseType = e.currentTarget.dataset.type
    if(app.globalData.isSupervising == true) {
      if(status == 1) {
        dd.navigateTo({
          url: '/subpages/pages/superviseContent/superviseContent',
        })
      }
    }else {
      dd.redirectTo({
        url: `/subpages/pages/schoolSelect/schoolSelect?superviseId=${superviseId}&superviseType=${superviseType}&type=1`,
      })
    }
    
  },
  endSupervise: function (e) {
    dd.confirm({
      title: '提示',
      content: '确定结束当前的巡查？',
      success: function (res) {
        console.log("res", res)
        if (res.confirm) {
          const userId = app.globalData.userId
          dd.httpRequest({
            url: app.baseUrl + '/DingDing/Supervise/endSupervise',
            method: "POST",
            headers: {
              "Content-Type": "application/json"
            },
            data: JSON.stringify({
              userId: userId,
              id: app.globalData.superviseId
            }),
            dataType: 'text',
            success: res => {
              if (res.data == "success") {
                dd.showToast({
                  content: '当前巡查任务已结束!'
                })
                setTimeout(() => {
                  dd.redirectTo({
                    url: `/subpages/pages/regularsupervise/regularsupervise`,
                  })
                }, 1000)
                app.globalData.isSupervising = false
                app.globalData.superviseSchoolId = 0
                app.globalData.superviseSchoolName = ""
                app.globalData.superviseId = 0
              }

            }
          })
        } else if (res.cancel) {
          console.log("取消")
        }
      }
    })
  }
})